package com.bluestone.context;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import com.bluestone.util.Util;

public class ContextBase implements IContext {
	
	String id = "";

	
	protected transient PropertyChangeSupport propertylisteners = new PropertyChangeSupport(
			this);
	
	protected HashMap<String,Object> map = new HashMap<String, Object>();		

	public Object get(String key) {
		return map.get(key);
	}

	public void put(String key, Object obj) {
		if(map.containsKey(key)){
			map.remove(key);
		}
		map.put(key,obj);
	}
	
	public String getStartTime(){
		return (String)map.get(STARTTIME);
	}
	
	public void setStartTime(String time){
		if(map.containsKey(STARTTIME)){
			map.remove(STARTTIME);
		}
		map.put(STARTTIME, time);
	}
	
	public String getEndTime(){
		return (String)map.get(ENDTIME);
	}
	
	public void setEndTime(String time){
		if(map.containsKey(ENDTIME)){
			map.remove(ENDTIME);
		}
		map.put(ENDTIME, time);
		
		//cal execute time
		String executeTime = ""+(Long.parseLong(time) - Long.parseLong(getStartTime()));
		if(map.containsKey(EXECUTETIIME)){
			map.remove(EXECUTETIIME);
		}
		map.put(EXECUTETIIME, executeTime);		
		
	}	
	
	public String getExecuteTime(){
		return (String)map.get(EXECUTETIIME);
	}	
	
	public String getExecuteResult(){
		return (String)map.get(EXECUTERESULT);
	}
	
	public void setExecuteResult(boolean result){
		if(map.containsKey(EXECUTERESULT)){
			map.remove(EXECUTERESULT);
		}		
		map.put(EXECUTERESULT,""+result);
	}	
	

	
	public ArrayList getExecuteLogs(){
		return (ArrayList)map.get(EXECUTELOGS);
	}	
	
	public void addExecuteLog(String log){
		
		Util.getLogger().info(log);
		ArrayList oldValue = getExecuteLogs();
		ArrayList list = null;
		if(!map.containsKey(EXECUTELOGS)){
			list = new ArrayList();
			list.add(log);
			map.put(EXECUTELOGS, list);
		}else{
			getExecuteLogs().add(log);
		}
		this.firePropertyChange(EXECUTELOGS, oldValue, getExecuteLogs());
		
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
	
	public void addContextListeners() {
		if(!ConfigContext.class.isInstance(this)){
			HashMap map = ConfigContext.getInstance().getContextListeners();
			if(map==null){
				return;
			}
			Iterator iterator =map.values().iterator();
			while(iterator.hasNext()){
				Object obj = iterator.next();
				PropertyChangeListener l = (PropertyChangeListener)obj;
				propertylisteners.addPropertyChangeListener(l);
			}
		}				
	}
	public void addPropertyChangeListener(PropertyChangeListener l) {
		this.addExecuteLog("add context property change listener "+ l.getClass().getName());
		propertylisteners.addPropertyChangeListener(l);
		
	}

	private void firePropertyChange(String prop, Object old, Object newValue) {
		propertylisteners.firePropertyChange(prop, old, newValue);
		
	}

	public void removePropertyChangeListener(PropertyChangeListener l) {
		propertylisteners.removePropertyChangeListener(l);
		
	}

}
